package com.hliang.andnga;

import java.io.*;
import java.net.URL;
import java.net.HttpURLConnection;
import java.util.List;
import java.util.Map;
import java.util.zip.GZIPInputStream;

class MyHttpClient
{
	private Map<String,List<String>> headerFields;
	private int responseCode;

	public int getResponseCode()
	{
		return responseCode;
	}

	public List<String> getHeaderField(String name)
	{
		if (headerFields==null)
			return null;
		else
			return headerFields.get(name);
	}

	public Map<String,List<String>> getHeaderFields()
	{
		return headerFields;
	}

	public byte[] GET(String url, Map<String,String> headers)
	{
		headerFields = null;
		responseCode = 0;

		URL _url = null;
		HttpURLConnection httpConn = null;
		byte[] result = null;
		try
		{
			_url = new URL(url);
			httpConn = (HttpURLConnection)_url.openConnection();
			httpConn.setRequestProperty("Accept-Encoding","gzip, deflate");
			if(headers!=null)
			{
				for(Object o: headers.keySet())
					httpConn.setRequestProperty((String)o, (String)headers.get(o));
			}

			InputStream in = httpConn.getInputStream();
			responseCode = httpConn.getResponseCode();
			headerFields = httpConn.getHeaderFields();
			if(responseCode==200)
			{
				int b=0;
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				String ce = httpConn.getContentEncoding();
				boolean gzip = ce==null ? false : ce.equals("gzip");
				if (gzip)
				{
					GZIPInputStream gzin = new GZIPInputStream(in);
					while( (b = gzin.read())!=-1)
						baos.write(b);
				}
				else
				{
					while( (b = in.read())!=-1)
						baos.write(b);
				}
				result = baos.toByteArray();
			}
			in.close();
		}
		catch (Exception e)
		{
			System.out.println(e);
		}
		finally
		{
			httpConn.disconnect();
		}
		return result;
	}

	public byte[] GET(String url)
	{
		return this.GET(url, null);
	}

	public byte[] POST(String url, String body, Map<String,String> headers)
	{
		headerFields = null;
		responseCode = 0;

		URL _url = null;
		HttpURLConnection httpConn = null;
		byte[] result = null;
		try
		{
			_url = new URL(url);
			httpConn = (HttpURLConnection)_url.openConnection();
			httpConn.setDoOutput(true);
			httpConn.setDoInput(true);
			httpConn.setRequestMethod("POST");
			httpConn.setUseCaches(false);
			httpConn.setInstanceFollowRedirects(false);
			httpConn.setRequestProperty("Accept-Encoding","gzip, deflate");
			httpConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
			httpConn.setRequestProperty("Content-Length",""+body.length());
			if(headers!=null)
			{
				for(Object o: headers.keySet())
					httpConn.setRequestProperty((String)o, (String)headers.get(o));
			}
			DataOutputStream out = new DataOutputStream(httpConn.getOutputStream());
			out.writeBytes(body);
			out.flush();
			out.close();
		
			InputStream in = httpConn.getInputStream();
			responseCode = httpConn.getResponseCode();
			headerFields = httpConn.getHeaderFields();
			if(responseCode==200)
			{
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				int b=0;
				if (httpConn.getContentEncoding().equals("gzip"))
				{
					GZIPInputStream gzin = new GZIPInputStream(in);
					while( (b = gzin.read())!=-1)
						baos.write(b);
				}
				else
				{
					while( (b = in.read())!=-1)
						baos.write(b);
				}
				result = baos.toByteArray();
			}
			in.close();
		}
		catch(IOException e)
		{
			System.out.println(e);
		}
		finally
		{
			httpConn.disconnect();
		}
		return result;
	}

	public byte[] POST(String url, String body)
	{
		return this.POST(url, body, null);
	}
}
